//
//  WDTaskDetailBasicSingleImageView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/29.
//

#import "WDTaskDetailBasicSingleImageView.h"
#import "WDArrivalServiceDetailModel.h"
#import "HZPhotoBrowser.h"

@interface WDTaskDetailBasicSingleImageView ()

@property (nonatomic, strong) WDArrivalServiceDetailModel *taskDetailModel;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, copy) NSString *title;

@end

@implementation WDTaskDetailBasicSingleImageView

- (void)initWithSubView {
    [super initWithSubView];
    
    
}

- (void)initWithTitleLabel {
    if (!_titleLabel) {
        CGRect frame = CGRectMake(20, 0, 102, 18);
        _titleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:13 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
            label.text = @"照片";
        }];
    }
//    self.titleLabel.text = objectNull(self.title);
}

- (void)updateWithViewData:(id)data {
    self.taskDetailModel = data;
    
    [self initWithTitleLabel];
    
    if (self.viewList.count == 3) {
        
        return;
    }
    float width = 80;
    float midSpace = (self.width - self.titleLabel.right - 20 - 3 * width) * 0.5;
    CGRect frame = CGRectMake(self.titleLabel.right, 0, width, width);
    UIImageView *preView = nil;
    for (int i=0; i<self.taskDetailModel.problemPhotoList.count; i++) {
        WDInspectUploadModel *model = self.taskDetailModel.problemPhotoList[i];
        if (i % 3 == 0) {
            if (preView) {
                frame.origin.x = self.titleLabel.right;
                frame.origin.y = preView.bottom + 5;
            } else {
                
            }
        } else {
            frame.origin.x = preView.right + midSpace;
        }
//        if (i != 0) {
//            frame.origin.x = preView.right + midSpace;
//        }
        UIImageView *picImgView = [UIImageView createWithImageView:self frame:frame defaultImage:nil propertiesBlock:^(UIImageView * _Nonnull imageView) {
            imageView.layer.cornerRadius = 8;
            imageView.layer.masksToBounds = YES;
//            imageView.backgroundColor = [UIColor redColor];
//            imageView.hidden = YES;
            imageView.userInteractionEnabled = YES;
        }];
        [picImgView sd_setImageWithURL:[NSURL URLWithString:model.uploadUrl] placeholderImage:nil];
        [self.viewList addObject:picImgView];
        preView = picImgView;
        
        @weakify(self);
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            @strongify(self);
            [self browseImageView:i view:picImgView];
        }];
        [picImgView addGestureRecognizer:tap];
    }
    
//    for (int i=0; i<self.taskDetailModel.problemPhotoList.count; i++) {
//        WDInspectUploadModel *model = self.taskDetailModel.problemPhotoList[i];
//        UIImageView *imgView = [self.viewList objectOrNilAtIndex:i];
//        if (imgView) {
//            imgView.hidden = NO;
//            [imgView sd_setImageWithURL:[NSURL URLWithString:model.uploadUrl] placeholderImage:nil];
//        }
//    }
    
    
    // 工单流程 * 检查 * 照片视频上传 inspectUploadMap
    
    // 用户反馈照片视频 problemUploadMap
    
    // 工单流程 * 报修 * 照片视频上传 repairUploadMap
    
}

- (void)browseImageView:(NSInteger)index view:(UIImageView *)view {
    
    NSMutableArray *addList = [NSMutableArray new];
    for (WDInspectUploadModel *ent in self.taskDetailModel.problemPhotoList) {
        [addList addObject:ent.uploadUrl];
    }
    
    // 图片预览
    HZPhotoBrowser *browser = [[HZPhotoBrowser alloc] init];
    browser.isNeedLandscape = NO;
    browser.imageArray = [addList copy];
    browser.currentImageIndex = (int)index;
    browser.imageCount = addList.count;
    if(addList.count == 1)//如果是一张图片tag 是200
    {
        browser.sourceImagesContainerView = view;
        browser.startTag = 200;
    }
    else//如果是多张图片tag 从100开始
    {
        browser.startTag = 100;
        browser.sourceImagesContainerView = view;
    }
    [browser show];
}

@end
